package br.ufrgs.inf.cwsmart.impl.matcher;

import java.util.List;
import java.util.Map;

import br.ufrgs.inf.cwsmart.exception.FrameworkException;
import br.ufrgs.inf.cwsmart.matcher.CWSProperties;
import br.ufrgs.inf.cwsmart.matcher.Matcher;

/**
 * 
 * @author Rodrigo S Lumertz
 *
 */
public class MatcherImpl extends Matcher {

	@Override
	protected CWSProperties instantiateCWSProperties(String language) 
		throws FrameworkException {

		if (language.equals("bpel")) {
			return new BPELProperties();
		}
		else {
			throw new FrameworkException(FrameworkException.ErrorCode.EXTENSIBLE_INSTANCE_NOT_FOUND);
		}
	}

	@Override
	protected boolean checkRule(Map<String, String> cwsProperties,
			Map<String, String> martRules) throws FrameworkException {
		
		boolean result = true;
		
		try {
			//application specific rules
			
			//rule 1
			String maxPartnerLinks = martRules.get("max_partner_link");
			if (!maxPartnerLinks.equals("any")) {
				Integer maxPartnersLK = Integer.parseInt(maxPartnerLinks);
				Integer cwsPartnerLK = Integer.parseInt(cwsProperties.get("partner_link_number"));
				
				if (cwsPartnerLK == null || cwsPartnerLK > maxPartnersLK) {
					return false;
				}
			}
			
			//rule 2
			String allowedExecType = martRules.get("execution_type");
			if (!allowedExecType.equals("all")) {				
				String cwsExecType = cwsProperties.get("cws_execution_type");
				
				if (cwsExecType == null || cwsExecType != allowedExecType) {
					return false;
				}
			}			
						
		} catch(Exception e) {
			throw new FrameworkException(FrameworkException.ErrorCode.UNKNOWN_MATCHER_ERROR);
		};
		
		return result;
	}

	@Override
	public boolean checkSemantic(List<String> cwsFunctionalities,
			List<String> martFunctionalities) throws FrameworkException {

		boolean result = true;
				
		if (!martFunctionalities.containsAll(cwsFunctionalities) || 
				!cwsFunctionalities.containsAll(martFunctionalities)) {
			
			result = false;
		}
				
		return result;
	}
}
